package net.ddns.xcyyds.kungfu.procedures;

import net.minecraftforge.registries.ForgeRegistries;

import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.Level;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.damagesource.EntityDamageSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.sounds.SoundSource;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.BlockPos;

import net.ddns.xcyyds.kungfu.network.KungFuModVariables;

import java.util.stream.Collectors;
import java.util.Random;
import java.util.List;
import java.util.Comparator;

public class LLaProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
		if (entity == null)
			return;
		double a = 0;
		double NL = 0;
		if ((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new KungFuModVariables.PlayerVariables())).quanFaLL) {
			NL = 6.5;
			if ((entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
					.orElse(new KungFuModVariables.PlayerVariables())).neiLi >= NL) {
				{
					double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
							.orElse(new KungFuModVariables.PlayerVariables())).neiLi - NL;
					entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
						capability.neiLi = _setval;
						capability.syncPlayerVariables(entity);
					});
				}
				if (entity instanceof Player _player && !_player.level.isClientSide())
					_player.displayClientMessage(new TextComponent("\u00A72\u00A7l\u4F60\u91CA\u653E\u4E86\uFF1A\u5D29\u62F3\u5F0F"), (true));
				{
					double _setval = (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
							.orElse(new KungFuModVariables.PlayerVariables())).quanFa + 0.8;
					entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
						capability.quanFa = _setval;
						capability.syncPlayerVariables(entity);
					});
				}
				if (entity instanceof LivingEntity _entity)
					_entity.swing(InteractionHand.MAIN_HAND, true);
				if (entity instanceof LivingEntity _entity)
					_entity.swing(InteractionHand.OFF_HAND, true);
				if (world instanceof Level _level) {
					if (!_level.isClientSide()) {
						_level.playSound(null, new BlockPos(x, y, z),
								ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.generic.explode")), SoundSource.PLAYERS, 1, 3);
					} else {
						_level.playLocalSound(x, y, z, ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.generic.explode")),
								SoundSource.PLAYERS, 1, 3, false);
					}
				}
				if (world instanceof ServerLevel _level)
					_level.sendParticles(ParticleTypes.CLOUD, (entity.getLookAngle().x * 2 + x), (entity.getLookAngle().y * 2 + y),
							(entity.getLookAngle().z * 2 + z), 40, 1, 1, 1, 0);
				entity.setDeltaMovement(new Vec3((entity.getLookAngle().x * 0.8), 0.1, (entity.getLookAngle().z * 0.8)));
				if (entity instanceof Player _player)
					_player.causeFoodExhaustion(1);
				if (entity instanceof LivingEntity _entity)
					_entity.addEffect(new MobEffectInstance(MobEffects.ABSORPTION, 80, 1, (false), (false)));
				{
					final Vec3 _center = new Vec3((entity.getLookAngle().x * 2 + x), (entity.getLookAngle().y * 2 + y),
							(entity.getLookAngle().z * 2 + z));
					List<Entity> _entfound = world.getEntitiesOfClass(Entity.class, new AABB(_center, _center).inflate(4 / 2d), e -> true).stream()
							.sorted(Comparator.comparingDouble(_entcnd -> _entcnd.distanceToSqr(_center))).collect(Collectors.toList());
					for (Entity entityiterator : _entfound) {
						if (!(entityiterator == entity)) {
							a = TwoEntityProcedure.execute(entity, entityiterator);
							entityiterator.setDeltaMovement(new Vec3(((entityiterator.getX() - x) / a), 0.8, ((entityiterator.getZ() - z) / a)));
							if (entityiterator instanceof LivingEntity _entity)
								_entity.hurt(new EntityDamageSource("bengquanshi", entity),
										(float) (7.5 + (entity.getCapability(KungFuModVariables.PLAYER_VARIABLES_CAPABILITY, null)
												.orElse(new KungFuModVariables.PlayerVariables())).neiLiMax
												* KungFuModVariables.MapVariables.get(world).neiLiMul));
							{
								ItemStack _ist = (entityiterator instanceof LivingEntity _entGetArmor
										? _entGetArmor.getItemBySlot(EquipmentSlot.FEET)
										: ItemStack.EMPTY);
								if (_ist.hurt(1, new Random(), null)) {
									_ist.shrink(1);
									_ist.setDamageValue(0);
								}
							}
							{
								ItemStack _ist = (entityiterator instanceof LivingEntity _entGetArmor
										? _entGetArmor.getItemBySlot(EquipmentSlot.LEGS)
										: ItemStack.EMPTY);
								if (_ist.hurt(1, new Random(), null)) {
									_ist.shrink(1);
									_ist.setDamageValue(0);
								}
							}
							{
								ItemStack _ist = (entityiterator instanceof LivingEntity _entGetArmor
										? _entGetArmor.getItemBySlot(EquipmentSlot.CHEST)
										: ItemStack.EMPTY);
								if (_ist.hurt(1, new Random(), null)) {
									_ist.shrink(1);
									_ist.setDamageValue(0);
								}
							}
							{
								ItemStack _ist = (entityiterator instanceof LivingEntity _entGetArmor
										? _entGetArmor.getItemBySlot(EquipmentSlot.HEAD)
										: ItemStack.EMPTY);
								if (_ist.hurt(1, new Random(), null)) {
									_ist.shrink(1);
									_ist.setDamageValue(0);
								}
							}
						}
					}
				}
			} else {
				if (entity instanceof Player _player && !_player.level.isClientSide())
					_player.displayClientMessage(new TextComponent("\u00A7c\u00A7l\u5185\u529B\u4E0D\u8DB3\uFF01"), (true));
			}
		} else {
			if (entity instanceof Player _player && !_player.level.isClientSide())
				_player.displayClientMessage(new TextComponent("\u00A7c\u00A7l\u4F60\u91CA\u653E\u7684\u62DB\u5F0F\u5C1A\u672A\u5B66\u4E60\uFF01"),
						(true));
		}
	}
}
